package com.tengju.user.application.user;

import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.application.shared.ApplicationExceptionCode;
import com.tengju.user.domain.model.user.*;
import com.tengju.user.domain.service.ApiStoreService;
import com.tengju.user.domain.service.HuaweiObsService;
import com.tengju.user.domain.service.RemoteUserService;
import com.tengju.user.domain.service.UserDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserInfoService {

    UserInfoRepository userInfoRepository;
    HuaweiObsService huaweiObsService;
    ApiStoreService apiStoreService;
    RemoteUserService remoteUserService;
    UserDataService userDataService;

    @Autowired
    public UserInfoService(UserInfoRepository userInfoRepository, HuaweiObsService huaweiObsService, ApiStoreService apiStoreService, RemoteUserService remoteUserService, UserDataService userDataService) {
        this.userInfoRepository = userInfoRepository;
        this.huaweiObsService = huaweiObsService;
        this.apiStoreService = apiStoreService;
        this.remoteUserService = remoteUserService;
        this.userDataService = userDataService;
    }

    @Value("${system.env}")
    String env;

    public Boolean changeNickname(UserId userId, String newName) throws UnsupportedEncodingException {

        UserInfo userInfo = userInfoRepository.getByUserId(userId);
        if (userInfo == null) {
            throw ApplicationException.error(ApplicationExceptionCode.INVALID_DATA, "未找到该用户");
        }
        userInfo.changeNickname(newName);
        userInfoRepository.updateNickname(userId, newName);
        return true;
    }

    public Boolean changeHeadImage(UserId userId, String headImage) {
        UserInfo userInfo = userInfoRepository.getByUserId(userId);
        if (userInfo == null) {
            throw ApplicationException.error(ApplicationExceptionCode.INVALID_DATA, "未找到该用户");
        }
        userInfo.changeHeadImage(headImage);
        userInfoRepository.updateHeadImage(userId, headImage);
        return true;
    }

    public String uploadHeadImage(MultipartFile multipartFile, UserId userId) {
        return huaweiObsService.uploadUserHeadImage(multipartFile, userId);
    }

    public void checkBankIdCard(String realName, String bankCard, String idCard) {
//        if (env.equals("dev")) {
//            return;
//        }
        apiStoreService.checkBankIdCard(realName, bankCard, idCard);
    }


    public void checkUserInfoForSign(String idCardName, String idCardNo) {
//        if (env.equals("dev")) {
//            return;
//        }
        apiStoreService.checkUserInfoForSign(idCardName, idCardNo);
    }


    public UserInfo getUserInfo(UserIdCode idCode) {
        if (idCode == null) {
            return null;
        }
        UserInfo userInfo = userInfoRepository.getByIdCode(idCode);
        if (userInfo == null) {
            userInfo = remoteUserService.getByIdCode(idCode);
        }
        return userInfo;
    }

    public DominationLevelEnum getDominationLevel(UserIdCode idCode) {

        UserInfo userInfo = getUserInfo(idCode);
        if (userInfo == null) {
            throw ApplicationException.error(ApplicationExceptionCode.INVALID_DATA, "用户不存在");
        }
        return DominationLevelEnum.parseByStarLevel(userInfo.starLevel());
    }


    public List<UserInfo> batchGetUserInfo(List<UserIdCode> idCodeList) {

        //查询服务商
        List<UserInfo> userInfos = userInfoRepository.batchGetUserInfo(idCodeList);
        //非服务商查询经销商
        if (CollectionUtils.isEmpty(userInfos) || userInfos.size() < idCodeList.size()) {
            List<UserIdCode> serviceProvider = userInfos.stream().map(UserInfo::getIdCode).collect(Collectors.toList());
            idCodeList = idCodeList.stream().filter(idCode -> !serviceProvider.contains(idCode)).distinct().collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(idCodeList)) {
                List<UserInfo> dealer = remoteUserService.getByIdList(idCodeList);
                userInfos.addAll(dealer);
            }
        }

        return userInfos;

    }


//    public static void main(String[] args) {
//        List<String> a = List.of("a", "b", "c");
//        List<String> b = List.of("b");
//        a.removeAll(b);
//        System.out.println(a);
//    }

    public UserDetail getUserDetail(UserIdCode idCode) {
        UserDetail userDetail = userDataService.getUserDetail(idCode);
        if (userDetail == null){
            return null;
        }
        //经销商 查询经销商user信息
        if (userDetail.getStarLevel() == StarLevelEnum.NONE) {
            UserInfo dealerInfo = remoteUserService.getByIdCode(idCode);
            userDetail.fillDealerInfo(dealerInfo);
        }
        return userDetail;
    }

}
